get random data laravel

49

laravel get random number of data from database -

Model::select('column')
    ->where('column','value')
    ->inRandomOrder()
    ->limit(2) // here is yours limit
    ->get();

laravel get random row -

User::all()->random();
User::all()->random(10); // The amount of items you wish to receive

get random data laravel -

// 5 indicates the number of records
User::inRandomOrder()->limit(5)->get();
// get one random record
User::inRandomOrder()->first();

Comments

Submit
0 Comments